#!/bin/bash

echo "请输入要检测的文件名: "
read FileName

if [ -e $FileName ]
then
    FileStatus=$(ls -l $FileName)
    # 显示文件当前属性
    echo "$FileStatus"
    FileSize=$(echo "$FileStatus" | awk '{print $5}')
    ChangeCount=0
    UnChangeCount=0
else
    echo "找不到文件[ $FileName ]! "
    exit 1
fi

while true
do
    sleep 5
    CurFileStatus=$(ls -l $FileName)
    CurFileSize=$(echo "$CurFileStatus" | awk '{print $5}')
    if [ $FileSize -eq $CurFileSize ]
    then
        UnChangeCount=$((UnChangeCount + 1))
        echo "文件大小未改变, 计数: $UnChangeCount"
        if [ $UnChangeCount -eq 10 ]
        then
            echo "已检测到文件大小连续10次未改变, 退出!"
            exit 0
        fi
    else
        ChangeCount=$((ChangeCount + 1))
        UnChangeCount=0
        FileSize=$CurFileSize
        echo "file [ $FileName ] size changed to [ $CurFileSize ]"
        if [ $ChangeCount -eq 2 ]
        then
            echo "文件大小已经累计变化两次, 退出! "
            exit 0
        fi
    fi
done